﻿// <copyright file="TrackMap.cs" company="Microsoft">
// Copyright (c) 2010 Microsoft All Rights Reserved
// </copyright>
// <author>Microsoft</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-01</date>
// <summary>Web analytics service behavior class definition</summary>

namespace Microsoft.WebAnalytics.BingMaps
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows;
    using System.Windows.Interactivity;
    using Microsoft.Maps.MapControl;
    using Microsoft.Maps.MapControl.Core;

    /// <summary>
    /// Track Control Events
    /// </summary>
    /// <remarks>You should use Control Tracking behaviors when raw event 
    /// tracking is not sufficient or you want to specify and use additional 
    /// parameters as properties.</remarks>
    public class TrackMap : Behavior<Map>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the event Logging method
        /// </summary>
        [Import("Log")]
        public Action<AnalyticsEvent> Log { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Attach events and initialize parts
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            CompositionInitializerEx.SatisfyImports(this);

            this.AssociatedObject.ViewChangeEnd += new EventHandler<Microsoft.Maps.MapControl.MapEventArgs>(this.AssociatedObject_ViewChangeEnd);

            this.AssociatedObject.Unloaded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
            {
                System.Windows.Interactivity.Interaction.GetBehaviors(this.AssociatedObject).Remove(this);
            });
        }

        /// <summary>
        /// Detach events
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.ViewChangeEnd -= new EventHandler<Microsoft.Maps.MapControl.MapEventArgs>(this.AssociatedObject_ViewChangeEnd);
        }

        #endregion

        #region Implementation
        /// <summary>
        /// Track the View Change End event
        /// </summary>
        /// <param name="sender">the associated object</param>
        /// <param name="e">the map event arugments</param>
        private void AssociatedObject_ViewChangeEnd(object sender, Microsoft.Maps.MapControl.MapEventArgs e)
        {
            foreach (var child in this.AssociatedObject.Children.Cast<MapShapeBase>())
            {
            }

            var logEvent = new AnalyticsEvent()
            {
                Name = "ViewChangeEnd",
                ObjectName = this.AssociatedObject.Name,
                ObjectType = this.AssociatedObject.GetType().Name
            };

            // Log the event
            this.Log(logEvent);
        }
        #endregion
    }
}
